package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.mongo.RecommendUserApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 今日佳人：
     *  db.recommend_user.find({userId:1}).sort({score:-1}).limit(1);
     * @param userId
     * @return
     */
    @Override
    public RecommendUser queryWithMaxScore(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.with(Sort.by(Sort.Order.desc("score"))).limit(1);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }

    /**
     * 查询今日推荐佳人分页对象
     //第一页， 每页10条
     db.recommend_user.find({userId:1}).sort({score:-1}).limit(10).skip(0);
     //第二页， 每页10条
     db.recommend_user.find({userId:1}).sort({score:-1}).limit(10).skip(10);
     //第三页， 每页10条
     db.recommend_user.find({userId:1}).sort({score:-1}).limit(10).skip(20);
     */
    @Override
    public PageResult queryRecommendation(Integer page, Integer pagesize, Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.with(Sort.by(Sort.Order.desc("score")));
        query.limit(pagesize).skip((page-1)*pagesize);

        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        Long count = mongoTemplate.count(query, RecommendUser.class);
        PageResult pageResult = new PageResult(page,pagesize,count.intValue(),recommendUserList);
        return pageResult;
    }

    @Override
    public Long queryScore(Long userId, Long recommendUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("recommendUserId").is(recommendUserId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if(recommendUser==null){
            return 60L;
        }
        return recommendUser.getScore().longValue();
    }

    @Override
    public List<RecommendUser> recommendTenUsers(Long userId) {
        List<RecommendUser> recommendUsers = mongoTemplate.find(new Query(Criteria.where("userId").is(userId)), RecommendUser.class);
        if (recommendUsers.size() <= 10) {
            return recommendUsers;
        }
        Collections.shuffle(recommendUsers);
        return recommendUsers.stream().limit(10).collect(Collectors.toList());
    }

    @Override
    public void removeTheRecommendFriend(Long userId, Long id) {
        Query query = new Query(Criteria.where("userId").is(userId).and("recommendUserId").is(id));
        mongoTemplate.remove(query,RecommendUser.class);
    }

    @Override
    public void changeFriend(Long userId, Long friendId) {
        Friend friend = new Friend();
        friend.setUserId(userId);
        friend.setFriendId(friendId);
        friend.setCreated(System.currentTimeMillis());
        mongoTemplate.save(friend);
        Friend friendHer = new Friend();
        friendHer.setUserId(friendId);
        friendHer.setFriendId(userId);
        friendHer.setCreated(System.currentTimeMillis());
        mongoTemplate.save(friendHer);
    }
}
